package com.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.common.Result;
import com.common.log.OperationLog;
import com.common.log.OperationType;
import com.entity.JobLevel;
import com.entity.Position;
import com.entity.Salary;
import com.service.SalaryService;
import com.service.sys.UserVoService;
import com.vo.sys.UserVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author clay
 * @since 2022-03-27
 */
@RestController
@RequestMapping("/salary")
public class SalaryController {

    @Resource
    private SalaryService salaryService;

    @ApiOperation("添加工资账套信息")
    @OperationLog(operationType = OperationType.INSERT, describe = "添加工资账套信息", entity = Salary.class, primaryKey = "id")
    @PostMapping(value = "/add")
    public Result addDepartment(@RequestBody @Valid Salary salary) {
        salaryService.save(salary);
        return Result.success();
    }

    @Resource
    private UserVoService userVoService;
    @ApiOperation("删除工资账套")
    @OperationLog(operationType = OperationType.DELETE, describe = "删除工资账套", entity = Salary.class, primaryKey = "id")
    @GetMapping(value = "/delete/{id}")
    public Result deleteDepartment(@PathVariable("id") String id) {
        List<UserVo> userVoList = userVoService.list(new QueryWrapper<UserVo>().eq(
                "salary_id", id
        ));
        if (CollectionUtil.isNotEmpty(userVoList)) {
            return Result.fail("存在被引用的信息，无法删除");
        }
        salaryService.removeById(id);
        return Result.success();
    }

    @ApiOperation("通过id获取工资账套信息")
    @GetMapping(value = "/info/{id}")
    public Result info(@PathVariable("id") String id) {
        return Result.success(salaryService.getById(id));
    }

    @ApiOperation("查询工资账套信息")
    @PostMapping(value = "/select")
    public Result selectDepartment(@RequestBody Salary salary) {
        QueryWrapper<Salary> wrapper = new QueryWrapper<>();
        wrapper.like(
                !StringUtils.isEmpty(salary.getName()),
                "name",
                salary.getName()
        );
        return Result.success(salaryService.list(wrapper));
    }

    @ApiOperation("修改工资账套")
    @PostMapping("/update")
    @OperationLog(operationType = OperationType.UPDATE, describe = "修改工资账套", entity = JobLevel.class, primaryKey = "id")
    public Result update(@RequestBody Salary salary) {
        return Result.success(salaryService.updateById(salary));
    }
}
